/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Figures;

import java.awt.Point;
import java.util.ArrayList;

/**
 *
 * @author Macias
 */
public class King extends Figures{
         boolean castleL,castleS;
    public King(boolean isWh,Point loc,boolean canCastleL,boolean canCastleS){
        location = loc;
        isWhite = isWh;
        castleL = canCastleL;
        castleS = canCastleS;

    }

    @Override
    public ArrayList<Point> checkPossibleMoves(char [][] board) {
        ArrayList<Point> possibleMoves = new ArrayList();

        for(int c = -1; c<=1;c++)
            for(int r=-1;r<=1;r++){
                if(c==0 && r==0)continue;
                if(isWhite){
                    if(location.x+c>=0 && location.x+c<=7 && location.y+r>=0 && location.y+r<=7)
                        if(board[location.x+c][location.y+r]<65 || board[location.x+c][location.y+r]>90){   //no our figures on that point
                            int cc,rr = -1;
                            boolean stop=false;
                            for( cc = -1; cc<=1 && !stop ;cc++)
                                for(rr=-1;rr<=1;rr++) //searching for oponent's king
                                    if(location.x+c+cc>=0 && location.x+c+cc <=7 && location.y+r+rr>=0 && location.y+r+rr<=7)
                                    {
                                        if (board[location.x + c + cc][location.y + r + rr] == 'k') {
                                            stop=true;
                                            break;
                                        }

                                    }
                            if(cc==2 && rr==2){
                               if(checkIfSafe(board,location.x+c,location.y+r,isWhite)) {
//                                   if(!this.isChecked(board,new Point(location.x+c,location.y+r)))
                                    possibleMoves.add(new Point(location.x+c,location.y+r));
                               }
                            }
                        }
                }
                else{
                    if(location.x+c>=0 && location.x+c<=7 && location.y+r>=0 && location.y+r<=7)
                        if(board[location.x+c][location.y+r]<97 || board[location.x+c][location.y+r]>122){   //no our figures on that point
                            int cc,rr = -1;
                            boolean stop=false;
                            for(cc = -1; cc<=1 && !stop ;cc++)
                                for(rr=-1;rr<=1;rr++) //searching for oponent's king
                                    if(location.x+c+cc>=0 && location.x+c+cc <=7 && location.y+r+rr>=0 && location.y+r+rr<=7)
                                        if(board[location.x+c+cc][location.y+r+rr]=='K'){
                                            stop=true;
                                            break;
                                        }
                            if(cc==2 && rr ==2)
                               if(checkIfSafe(board,location.x+c,location.y+r,isWhite))
//                                   if(!this.isChecked(board,new Point(location.x+c,location.y+r)))
                                       possibleMoves.add(new Point(location.x+c,location.y+r));
                        }
                }
            }
        
//        System.out.println("ilosc ruchow krola "+possibleMoves.size());
//        for(int i =0; i<possibleMoves.size();i++)
//            System.out.println("ruch "+possibleMoves.get(i).x + " , "+possibleMoves.get(i).y);

//        for(int i =0; i<possibleMoves.size();i++){
//            if(this.isChecked(board, possibleMoves.get(i)))
//                possibleMoves.remove(i);
//            i--;
//        }

        return possibleMoves;
    }
    @Override
    public int getType(){
        if(isWhite) return 75;
        else return 107;

    }

    private boolean checkIfSafe(char[][] tmpBoard, int x, int y,boolean isWhite) {
        int n = 1 ;
        boolean s1= false,s2= false,s3= false,s4= false,s5= false;
        boolean s6= false,s7= false,s8= false;
        if(isWhite){
            while(n<=7){
                //W
                if(!s1){
                    if(x-n >=0){
                        if(tmpBoard[x-n][y]!='0'){
                            switch(tmpBoard[x-n][y]){
                                default:
                                    s1 = true;
                                            break;
                                case 'r': case 'q':
                                    return false;
                            }
                        }
                    }else s1=true;
                }
                //NW
                if(!s2){
                    if(x-n >=0 && y-n>=0){
                        if(tmpBoard[x-n][y-n]!='0'){
                            switch(tmpBoard[x-n][y-n]){
                                default:
                                         s2 = true;
                                            break;
                                case 'p': if(n==1) return false;
                                          else{
                                            s2=true;
                                            break;
                                        }
                                case 'b':
                                case 'q':return false;

                            }
                        }
                    }else s2=true;
                }
                //N
                if(!s3){
                    if(y-n >=0){
                        if(tmpBoard[x][y-n]!='0'){
                            switch(tmpBoard[x][y-n]){
                                default:
                                    s3 = true;
                                            break;
                                case 'r':
                                case 'q':return false;

                            }
                        }
                    }else s3=true;
                }
                //NE
                if(!s4){
                    if(x+n <=7 && y-n>=0){
                        if(tmpBoard[x+n][y-n]!='0'){
                            switch(tmpBoard[x+n][y-n]){
                                default:
                                         s4 = true;
                                            break;
                                case 'p': if(n==1) return false;
                                          else{
                                            s4=true;
                                            break;
                                        }
                                case 'b':
                                case 'q':return false;

                            }
                        }
                    }else s4=true;
                }
                //E
                if(!s5){
                    if(x+n <=7){
                        if(tmpBoard[x+n][y]!='0'){
                            switch(tmpBoard[x+n][y]){
                                default:
                                    s5 = true;
                                            break;
                                case 'r':
                                case 'q':return false;

                            }
                        }
                    }else s5=true;
                }
                //SW
                if(!s6){
                    if(x-n >=0 && y+n<=7){
                        if(tmpBoard[x-n][y+n]!='0'){
                            switch(tmpBoard[x-n][y+n]){
                                default:
                                         s6 = true;
                                            break;
                                case 'p': if(n==1) return false;
                                          else{
                                            s6=true;
                                            break;
                                        }
                                case 'b':
                                case 'q':return false;

                            }
                        }
                    }else s6=true;
                }
                //S
                if(!s7){
                    if(y+n <=7){
                        if(tmpBoard[x][y+n]!='0'){
                            switch(tmpBoard[x][y+n]){
                                default:
                                    s7 = true;
                                            break;
                                case 'r':
                                case 'q':return false;

                            }
                        }
                    }else s7=true;
                }
                   //SE
                if(!s8){
                    if(x+n <=7 && y+n<=7){
                        if(tmpBoard[x+n][y+n]!='0'){
                            switch(tmpBoard[x+n][y+n]){
                                default:
                                         s8 = true;
                                            break;
                                case 'p': if(n==1) return false;
                                          else{
                                            s8=true;
                                            break;
                                        }
                                case 'b':
                                case 'q':return false;

                            }
                        }
                    }else s8=true;
                }


                 n++;
            }
                //checking for oponents knight 0
                if(x-2>=0 && y-1>=0)
                    if(tmpBoard[x-2][y-1]=='n') return false;
                if(x-1>=0 && y-2>=0)
                    if(tmpBoard[x-1][y-2]=='n') return false;
                if(x+2<=7 && y-1>=0)
                    if(tmpBoard[x+2][y-1]=='n') return false;
                if(x+1<=7 && y-2>=0)
                    if(tmpBoard[x+1][y-2]=='n') return false;

                if(x-2>=0 && y+1<=7)
                    if(tmpBoard[x-2][y+1]=='n') return false;
                if(x-1>=0 && y+2<=7)
                    if(tmpBoard[x-1][y+2]=='n') return false;
                if(x+2<=7 && y+1<=7)
                    if(tmpBoard[x+2][y+1]=='n') return false;
                if(x+1<=7 && y+2<=7)
                    if(tmpBoard[x+1][y+2]=='n') return false;


        }

        //##################### checking black player ####################
        else{
            while(n<=7){
                //W
                if(!s1){
                    if(x-n >=0){
                        if(tmpBoard[x-n][y]!='0'){
                            switch(tmpBoard[x-n][y]){
                                default:
                                    s1 = true;
                                            break;
                                case 'R': case 'Q': return false;
                            }
                        }
                    }else s1=true;
                }
                //SW
                if(!s2){
                    if(x-n >=0 && y+n<=7){
                        if(tmpBoard[x-n][y+n]!='0'){
                            switch(tmpBoard[x-n][y+n]){
                                default:
                                         s2 = true;
                                            break;
                                case 'P': if(n==1) return false;
                                          else{
                                            s2=true;
                                            break;
                                        }
                                case 'B':
                                case 'Q': return false;

                            }
                        }
                    }else s2=true;
                }
                //S
                if(!s3){
                    if(y+n <=7){
                        if(tmpBoard[x][y+n]!='0'){
                            switch(tmpBoard[x][y+n]){
                                default:
                                    s3 = true;
                                            break;
                                case 'R':
                                case 'Q': return false;

                            }
                        }
                    }else s3=true;
                }
                //SE
                if(!s4){
                    if(x+n <=7 && y+n<=7){
                        if(tmpBoard[x+n][y+n]!='0'){
                            switch(tmpBoard[x+n][y+n]){
                                default:
                                         s4 = true;
                                            break;
                                case 'P': if(n==1) return false;
                                          else{
                                            s4=true;
                                            break;
                                        }
                                case 'B':
                                case 'Q': return false;

                            }
                        }
                    }else s4=true;
                }
                //E
                if(!s5){
                    if(x+n <=7){
                        if(tmpBoard[x+n][y]!='0'){
                            switch(tmpBoard[x+n][y]){
                                default:
                                    s5 = true;
                                            break;
                                case 'R':
                                case 'Q': return false;

                            }
                        }
                    }else s5=true;
                }
                //NW
                if(!s6){
                    if(x-n >=0 && y-n>=0){
                        if(tmpBoard[x-n][y-n]!='0'){
                            switch(tmpBoard[x-n][y-n]){
                                default:
                                         s6 = true;
                                            break;
                                case 'P': if(n==1) return false;
                                          else{
                                            s6=true;
                                            break;
                                        }
                                case 'B':
                                case 'Q': return false;

                            }
                        }
                    }else s6=true;
                }
                 //N
                if(!s7){
                    if(y-n >=0){
                        if(tmpBoard[x][y-n]!='0'){
                            switch(tmpBoard[x][y-n]){
                                default:
                                    s7 = true;
                                            break;
                                case 'R':
                                case 'Q': return false;

                            }
                        }
                    }else s7=true;
                }
                //NE
                if(!s8){
                    if(x+n <=7 && y-n>=0){
                        if(tmpBoard[x+n][y-n]!='0'){
                            switch(tmpBoard[x+n][y-n]){
                                default:
                                         s8 = true;
                                            break;
                                case 'P': if(n==1) return false;
                                          else{
                                            s8=true;
                                            break;
                                        }
                                case 'B':
                                case 'Q': return false;

                            }
                        }
                    }else s8=true;
                }



                n++;
            }
        //checking for oponents knight 0
                if(x-2>=0)
                    if(tmpBoard[x-2][y+1]=='N') return false;
                if(x-1>=0)
                    if(tmpBoard[x-1][y+2]=='N') return false;
                if(x+2<=7)
                    if(tmpBoard[x+2][y+1]=='N') return false;
                if(x+1<=7)
                    if(tmpBoard[x+1][y+2]=='N') return false;

                if(x-2>=0 && y+1<=7)
                    if(tmpBoard[x-2][y+1]=='N') return false;
                if(x-1>=0 && y+2<=7)
                    if(tmpBoard[x-1][y+2]=='N') return false;
                if(x+2<=7 && y+1<=7)
                    if(tmpBoard[x+2][y+1]=='N') return false;
                if(x+1<=7 && y+2<=7)
                    if(tmpBoard[x+1][y+2]=='N') return false;
       }
        if(s1 && s2 && s3 && s4 && s5 && s6 && s7 && s8 )return true;
        else return false;
    }

}
